//@login & register
const express=require("express");
const router=express.Router();//实例化

//引入passport
const passport=require("passport");

//引入User
const User=require("../../models/Profile");
const e = require("express");
const keys = require("../../config/keys");
const Profile = require("../../models/Profile");
const { options, route } = require("./users");

//$router GET api/profiles/test
//@desc 返回请求的json数据
//@access public
//当访问localhost/test的时候返回
router.get("/test",(req,res)=>{
    res.json({msg:"profile works"})
})


//$router POST api/profiles/add
//@desc 创建信息接口
//@access private
router.post("/add",passport.authenticate('jwt',{session:false}),(req,res)=>{
    const profilesFields={};

    if(req.body.type) profilesFields.type=req.body.type;
    if(req.body.describe) profilesFields.describe=req.body.describe;
    if(req.body.income) profilesFields.income=req.body.income;
    if(req.body.expend) profilesFields.expend=req.body.expend;
    if(req.body.cash) profilesFields.cash=req.body.cash;
    if(req.body.remark) profilesFields.remark=req.body.remark;

    new Profile(profilesFields).save().then(Profile=>{
        res.json(Profile);
    })
})


//$router POST api/profiles/
//@desc 获取所有信息
//@access private
router.get("/",passport.authenticate("jwt",{session:false}),(req,res)=>{
    Profile.find()
        .then(profile=>{
            if(!profile){
                return res.status(404).json('没有任何内容');
            }

            res.json(profile);
        })
        .catch(err=>res.status(404).json(err));
})


//$router POST api/profiles/id
//@desc 获取单个信息
//@access private
router.get("/:id",passport.authenticate("jwt",{session:false}),(req,res)=>{
    Profile.findById({_id:req.params.id})
           .then(profile=>{
               if(!profile){
                   return res.status(404).json("没有任何内容");
               }

               res.json(profile);
           })
           .catch(err=>res.status(404).json(err));
});


//$router POST api/profiles/edit/id
//@desc 编辑信息
//@access private
router.post("/edit/:id",passport.authenticate('jwt',{session:false}),(req,res)=>{
    const profilesFields={};

    if(req.body.type) profilesFields.type=req.body.type;
    if(req.body.describe) profilesFields.describe=req.body.describe;
    if(req.body.income) profilesFields.income=req.body.income;
    if(req.body.expend) profilesFields.expend=req.body.expend;
    if(req.body.cash) profilesFields.cash=req.body.cash;
    if(req.body.remark) profilesFields.remark=req.body.remark;

    Profile.findOneAndUpdate(
        {_id:req.params.id},
        {$set:profilesFields},
        {new:true}
    ).then(profile=>res.json(profile));

})


//$router POST api/profiles/delete/id
//@desc 删除信息
//@access private
router.delete("/delete/:id",passport.authenticate("jwt",{session:false}),
(req,res)=>{
    console.log(req.params.id);
    Profile.findOneAndRemove(req.params.id, (err, doc) => {
        if(!err) {
             res.send(doc);
             }
        else {
            console.log('Error in Insurance agent Delete :'
             + JSON.stringify(err, undefined, 2)); 
        }


    });
});

module.exports=router;